clear all 
set more off

* This code creates Figure 4
* heat map of 3-qtr exit rates from self-emp

// load
use "Transitions_3Qtr_BWUN.dta", clear


* set the relevant transitions to keep
local t0list "BB BW BU BN WB WW WU WN UB UW UU UN NB NW NU NN"
local t1list "B W U N"



foreach t0 of local t0list {
	foreach t1 of local t1list {

		gen _hold = ytrate if trans == "`t0'`t1'" 
		bys ccode year: egen T_`t0'`t1' = max(_hold)
		drop _hold

	}
}

duplicates drop ccode year, force
drop ytrate


* ------------ HEAT MAP 
* ------------ IF MBW: MB, BW --> B, W, M
* ------------ IF BWUN, make a bunch

local rnn1 = 3
local rnn2 = 4	

matrix heat2 = J(`rnn1',`rnn2',.)
matrix heat2p = J(`rnn1',`rnn2',.)
matrix heat2ns = J(`rnn1',`rnn2',.)
matrix heat2se = J(`rnn1',`rnn2',.)


// NOTE: THESE ARE IN VERY PARTICULAR ORDER. DO NOT CHANGE.
// NOTE 2: MOST OF THIS IS JUST ABOUT GETTING STARS AND COLORS RIGHT.

local rn = 1
foreach x in UB NB BW {

	local cn = 1
	foreach z in U N B W {
		reg T_`x'`z' lcgdp, cluster(ccode)
		matrix heat2[`rn',`cn'] = _b[lcgdp]
		matrix heat2p[`rn',`cn'] = (2 * ttail(e(df_r), abs(_b[lcgdp]/_se[lcgdp])))
		matrix heat2se[`rn',`cn'] = round(_se[lcgdp],0.001)

		local cn = `cn'+1
	}

	local rn = `rn' + 1
}



// Set heat1ns = . if not significant
forvalues i = 1/`rnn1' {
	forvalues j = 1/`rnn2' {

		if heat2p[`i',`j'] <= 0.10 {
			matrix heat2ns[`i',`j'] = heat2[`i',`j']
		}

	}
}


// Set significance stars
forvalues i = 1/`rnn1' {
	forvalues j = 1/`rnn2' {


		local se_`i'`j' = heat2se[`i',`j']
		local yse_`i'`j' = `i' + 0.15
		local xse_`i'`j' = `j' - 0

		// set the values for location of stars
		// y does not depend on anything. x value depends on stars
		local y_`i'`j' = `i' - 0.01



		// Compute the stars and x location-- THIS DEPENDS ON SIZE OF THE MATRIX

		if heat2p[`i',`j'] > 0.10 {
			local x_`i'`j' = `j' + 0.21
			local star_`i'`j' ""
		}


		if heat2p[`i',`j'] > 0.05 & heat2p[`i',`j'] <= 0.10 {
			local star_`i'`j' "*" 

			// set x location if p in (0.01, 0.05]
			if heat2[`i',`j'] < 0 {
				local x_`i'`j' = `j' + 0.1
			}	
			else {
				local x_`i'`j' = `j' + 0.08
			}				
		}

		if heat2p[`i',`j'] > 0.01 & heat2p[`i',`j'] <= 0.05 {
			local star_`i'`j' "**" 

			// set x location if p in (0.01, 0.05]
			if heat2[`i',`j'] < 0 {
				local x_`i'`j' = `j' + 0.27
			}	
			else {
				local x_`i'`j' = `j' + 0.26
			}				
		}	

		if heat2p[`i',`j'] < 0.01 {
			local star_`i'`j' "***" 

			// set x location if p < 0.01
			if heat2[`i',`j'] < 0 {
				local x_`i'`j' = `j' + 0.30
			}	
			else {
				local x_`i'`j' = `j' + 0.27
			}			
		}	
	}
}


matrix rownames heat2 = U,S N,S S,W
matrix colnames heat2 = U N S W

matrix rownames heat2p = US NS SW
matrix colnames heat2p = U N S W

matrix rownames heat2ns = US NS SW
matrix colnames heat2ns = U N S W


colorpalette HSV heat, n(15) reverse nograph
*values(format(%9.2f)) colors(`r(p)',intensity(0.6))

* HCL pinkgreen 

#delimit ;
heatplot heat2, 
values(format(%9.3f) mlabsize(medium)) colors(HSV bluered2,intensity(1))
cuts(-0.15(0.02)0.15)
graphregion(color(white)) p(lcolor(black%50) lwidth(vthin))
xtitle("To:") ytitle("From:") 
xscale(alt lstyle(none)) xlabel(,tlength(0))
yscale(lstyle(none)) ylabel(,tlength(0) nogrid)
legend(off)
name(heat_2UN)

text(`y_11' `x_11' "`star_11'")
text(`y_12' `x_12' "`star_12'")
text(`y_13' `x_13' "`star_13'")
text(`y_14' `x_14' "`star_14'")

text(`y_21' `x_21' "`star_21'")
text(`y_22' `x_22' "`star_22'")
text(`y_23' `x_23' "`star_23'")	
text(`y_24' `x_24' "`star_24'")	

text(`y_31' `x_31' "`star_31'")
text(`y_32' `x_32' "`star_32'")
text(`y_33' `x_33' "`star_33'")	
text(`y_34' `x_34' "`star_34'")

text(`yse_11' `xse_11' "(0`se_11')", size(medium))
text(`yse_12' `xse_12' "(0`se_12')", size(medium))
text(`yse_13' `xse_13' "(0`se_13')", size(medium))
text(`yse_14' `xse_14' "(0`se_14')", size(medium))

text(`yse_21' `xse_21' "(0`se_21')", size(medium))
text(`yse_22' `xse_22' "(0`se_22')", size(medium))
text(`yse_23' `xse_23' "(0`se_23')", size(medium))
text(`yse_24' `xse_24' "(0`se_24')", size(medium))

text(`yse_31' `xse_31' "(0`se_31')", size(medium))
text(`yse_32' `xse_32' "(0`se_32')", size(medium))
text(`yse_33' `xse_33' "(0`se_33')", size(medium))
text(`yse_34' `xse_34' "(0`se_34')", size(medium))		
;
#delimit cr
graph export "Figures/Figure4.tif", replace
graph export "Figures/pdf_figures/Figure4.pdf", replace















